$(document).ready(function(){   
    //查询公告
    $.ajax({
        type:'POST',                                            //请求的方式
        url:'http://localhost:8066/news/findAll',           //请求地址
        dataType: 'json',
        data:'{}',                                              //发送的数据
        contentType: "application/json; charset=utf-8",
        success:function (res) {
                if(res.data!==null){
                        var len=res.data.length;
                        var rowBtn=document.getElementById('row-btn');
                        for(var i=0;i<len;i++){
                            if((i+1)%2!=0){
                                var row=document.createElement('div');
                                row.setAttribute('class','row rowset');
                                if(row){
                                    insertAfter(row,rowBtn.lastChild)
                                }else{
                                    insertAfter(row,rowBtn);
                                }
                            }
                            var col=document.createElement('div');
                            col.setAttribute('class','col-md-6');
                            var panel=document.createElement('div');
                            panel.setAttribute('class','panel');
                            var panelHead=document.createElement('div');
                            panelHead.setAttribute('class','panel-heading head-org');
                            var tit=document.createTextNode(res.data[i].title);
                            panelHead.appendChild(tit);
                            panel.appendChild(panelHead);
                            var panelBody=document.createElement('div');
                            panelBody.setAttribute('class','panel-body');
                            var p=document.createElement('p');
                            var txt=document.createTextNode(res.data[i].context);
                            p.appendChild(txt);
                            panelBody.appendChild(p);
                            panel.appendChild(panelBody);
                            col.appendChild(panel);
                            row.appendChild(col);


                            /*选择框*/
                            var absol=document.createElement('div');
                            absol.setAttribute('class','absol');
                            var input=document.createElement('input');
                            input.setAttribute('type','checkbox');
                            input.setAttribute('class','flat');
                            input.setAttribute('style','display:none;');
                            input.setAttribute('id','informbox'+res.data[i].id);

                            var label=document.createElement('label');
                            label.setAttribute('for','informbox'+res.data[i].id);
                            label.setAttribute('class','labelchk');
                            label.setAttribute('style','display:none;');
                            absol.appendChild(input);
                            absol.appendChild(label);
                            col.appendChild(absol);

                        }
                }

        },
        beforeSend:function (argument) {                        // 在发送请求之前调用,可以做一些验证之类的处理

        },
        error:function (err) {                                  //请求失败调用
                console.log(err);
                console.log("请求错误");
        },
    });

    //添加公告
    $('#submitinf').click(function(){

        var content= $('#pubContent').val();
        $.ajax({
            type: 'post',
            url: 'http://117.78.51.36/taxer/news/insert',
            data: '{"context":"'+content+'"}',
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function(result) {
                if(result.data==1){
                    location.reload();
                }
            }
        });
        $('#informModal').modal('hide');

    });

    //删除公告
    $('#delSure').click(function(){

        var inp=document.getElementsByTagName('input');
        var n=inp.length;
        var res="";
        for(var i=0;i<n;i++){
            if(inp[i].type=='checkbox' && inp[i].checked){
                var str=inp[i].getAttribute('id');
                var arr=str.split("informbox");
                var res=res+'{"id":'+arr[1]+'},'
            }
        }
        res=res.substring(0,res.length-1);

        $.ajax({
            type: 'post',
            url: 'http://117.78.51.36/taxer/news/deleteSome',
            data: '['+res+']',
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function(result) {
                if(result.data==1){
                    location.reload();
                }
            }
        });
        $('#delModal').modal('hide');

    });



}); 
